import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter_immersion_bar/flutter_immersion_bar.dart';

void main(){
  FlutterImmersionBar.api.statusBarAlpha(0);
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _ColorConfig {
  final Color title;
  final Color ico;
  final Color background;

  _ColorConfig(this.title, this.ico, this.background);
}

class _MyAppState extends State<MyApp> {
  _ColorConfig _colorConfig =
      _ColorConfig(Colors.white, Colors.white, Colors.blue);

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {}

  Widget buildBody() {
    return Container(
      child: Column(
        children: <Widget>[
          FlatButton(
            child: Text("灰色"),
            onPressed: () async {
              if (await FlutterImmersionBar.api.statusBarDarkFont(true, 0.1)) {
                setState(() {
                  _colorConfig = _ColorConfig(
                      Color(0xff131f35), Color(0xff76798c), Color(0xfff0f2f7));
                });
              }
            },
          ),
          FlatButton(
            child: Text("蓝色"),
            onPressed: () async {
              if (await FlutterImmersionBar.api.statusBarDarkFont(false, 0)) {
                setState(() {
                  _colorConfig =
                      _ColorConfig(Colors.white, Colors.white, Colors.blue);
                });
              }
            },
          )
        ],
      ),
    );
  }

  Widget _buildScaffold() {
    //f0f2f7
    var appTextTheme = Theme.of(context).primaryTextTheme;
    var appIconTheme = Theme.of(context).primaryIconTheme;
    var textTheme = appTextTheme.copyWith(
        title: appTextTheme.title.copyWith(color: _colorConfig.title));
    var iconTheme = appIconTheme.copyWith(color: _colorConfig.ico);
    return Scaffold(
      backgroundColor: _colorConfig.background,
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        elevation: 0,
        textTheme: textTheme,
        backgroundColor: _colorConfig.background,
        title: Text(
          "附近门店",
        ),
        centerTitle: true,
        iconTheme: iconTheme,
      ),
      body: buildBody(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: _buildScaffold(),
    );
  }
}
